package com.burgeon.backend.EnterpriseWeChat.rest.Express;

import com.burgeon.backend.EnterpriseWeChat.entity.ExpressCompany;
import com.burgeon.backend.EnterpriseWeChat.entity.Vip;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class getExpressCompany extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public getExpressCompany(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public ArrayList<ExpressCompany> getData() throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(), url);
//        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        String count = new JSONArray(str).getJSONObject(0).getString("count");
        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }
//        HashMap map = new HashMap();
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<ExpressCompany> expressCompanies= new ArrayList<>();
        if(jsonArray.length()>0){
            for (int i = 0; i <jsonArray.length() ; i++) {
                ExpressCompany expressCompany = new ExpressCompany();
                expressCompany.setExpressName(jsonArray.getJSONArray(i).getString(0));
                expressCompany.setExpressPrice(jsonArray.getJSONArray(i).getString(1));
                expressCompany.setExpressId(jsonArray.getJSONArray(i).getString(2));
               expressCompanies.add(expressCompany);
            }
        }

        return expressCompanies;
    }

    private MultiValueMap Data() {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段
        columns.add("NAME");
        columns.add("PRICE");
        columns.add("ID");

        params.put("columns", columns);
        params.put("table", "C_EXPRESSCOMPANY");//表名
        params.put("count", "true");

//        System.err.println(paramss);
        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        getExpressCompany g = new getExpressCompany("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(g.getData());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
